#include "StdAfx.h"
#include "cln_move_handler.h"
namespace fly {

	namespace jld{

		namespace client {	

			cln_move_handler::cln_move_handler(bool is_test)
				:cln_handler(is_test)
			{

			}

			bool cln_move_handler::handle_recv(recv_buf& recv_buf, send_buf& send_buf, cln_info& info)
			{
				if (MOVE != recv_buf.pid())
				{
					return cln_handler::handle_recv(recv_buf, send_buf, info);
				}
				LOG_INFO("Handle move");
				Move move;
				if (!recv_buf.parse_from_recv(move))
				{
					LOG_ERR("Parse move faild!");
					return false;
				}
				if (!is_test_)
				{
					LOG_DEBUG("Untest move");
					return true;
				}
				State* state = move.mutable_state();
				state->set_cur_time(timestamp->cur_mill_sec());
				Vector* cur_pos = state->mutable_cur_pos();
				Vector* pur_pos = state->mutable_pur_pos();

				LOG_DEBUG("uid:", info.account());
				LOG_DEBUG("move uid:", move.uid(), "[cur(x,y):(", pantheios::real(cur_pos->x()), ",",
					pantheios::real(cur_pos->y()),")->pur(x,y):(", pantheios::real(pur_pos->x()), ",",
					pantheios::real(pur_pos->y()), ")]");

				if (move.uid() == info.account())
				{
					cur_pos->set_x(pur_pos->x());
					cur_pos->set_y(pur_pos->x());

					pur_pos->set_x(cur_pos->x() + 1);
					pur_pos->set_y(cur_pos->y() + 2);
				}

				Sleep((int) (pur_pos->y()) * 10);
				return send_buf.serialize_to_send(MOVE, target_, move);
			}
		}
	}
}